from app.pdf_extract_kit.registry.registry import TASK_REGISTRY
from app.pdf_extract_kit.tasks.base_task import BaseTask

TASK_NAME = "layout_detection"

@TASK_REGISTRY.register(TASK_NAME)
class LayoutDetectionTask(BaseTask):

    @property
    def task_name(self):
        return TASK_NAME

    def __init__(self, model):
        super().__init__(model)

    def predict_images(self, input_data):
        """
        Predict layouts in images.

        Args:
            input_data (str): Path to a single image file or a directory containing image files.
            result_path (str): Path to save the prediction results.

        Returns:
            list: List of prediction results.
        """
        images = self.load_images(input_data)
        # Perform detection
        return self.model.predict(images)

    def predict_pdfs(self, input_data, visualize=False, result_path=None):
        """
        Predict layouts in PDF files.

        Args:
            input_data (str): Path to a single PDF file or a directory containing PDF files.
            result_path (str): Path to save the prediction results.

        Returns:
            list: List of prediction results.
        """
        pdf_images, base_name = self.load_pdf_images(input_data)
        results = self.model.predict(pdf_images)
        if visualize and result_path:
            keys = [f"{base_name}_page{idx+1:04d}" for idx in range(len(pdf_images))]
            self.model.visualize_image(results, keys, result_path)
        # Perform detection
        return results

    def process_flow(self, iamges: list):
        return self.model.predict(iamges)
